﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Reviewer {
    public partial class FrmSearchMoves : Form {
        private FrmMain frmMain;
        private string pid;
        private string gameType;
        private DBHelper dbHelper = new DBHelper();

        public FrmSearchMoves(FrmMain frmMain) {
            InitializeComponent();
            this.Icon = Icon.FromHandle(Properties.Resources.find.GetHicon());
            this.frmMain = frmMain;
            dgvResults.AutoGenerateColumns = false;
        }

        private void FrmSearchMoves_Load(object sender, EventArgs e) {

        }

        public void Search(string pid, int moveSide, string gameType) {
            this.Cursor = Cursors.WaitCursor;
            this.pid = pid;
            this.gameType = gameType;
            this.Text = "Seach Moves (" + (moveSide == Constants.SIDE_WHITE ? "White" : "Black") + ")";

            string[] moveArray;

            DataTable dt = dbHelper.GetMoves(pid, moveSide, gameType);
            for (int i = 0; i < dt.Rows.Count; i++) {
                moveArray = dt.Rows[i]["Move"].ToString().Split(',');
                dt.Rows[i]["MoveName"] = GetPieceName(Int32.Parse(moveArray[0])) + "(" + moveArray[3] + "," + moveArray[4] + ")";
            }
            dgvResults.DataSource = dt;
            this.Cursor = Cursors.Default;
        }

        /// <summary>
        /// 获取棋子的名称
        /// </summary>
        /// <param name="pIndex">棋子的序号</param>
        /// <returns></returns>
        private string GetPieceName(int pIndex) {
            switch (pIndex) {
                case 0:
                case 13: return "Q";
                case 1:
                case 14: return "A3";
                case 2:
                case 15: return "A2";
                case 3:
                case 16: return "A1";
                case 4:
                case 17: return "G3";
                case 5:
                case 18: return "G2";
                case 6:
                case 19: return "G1";
                case 7:
                case 20: return "B2";
                case 8:
                case 21: return "B1";
                case 9:
                case 22: return "S2";
                case 10:
                case 23: return "S1";
                case 11:
                case 24: return "M";
                case 12:
                case 25: return "L";
            }
            return string.Empty;
        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) {
            if (e.ColumnIndex > 0) {
                int index = e.Value.ToString().IndexOf("%");
                if (index > 0) {
                    float wins = float.Parse(e.Value.ToString().Substring(0, index));
                    if (wins > 50) {
                        e.CellStyle.ForeColor = Color.Red;
                    }
                }
            }
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e) {
            if(e.RowIndex >= 0) {
                frmMain.PlaySearchMove(dgvResults.Rows[e.RowIndex].Cells["Move2"].Value.ToString());
            }
        }

        private void searchForwardToolStripMenuItem_Click(object sender, EventArgs e) {
            if(dgvResults.CurrentCell != null) {
                frmMain.PlaySearchMove(dgvResults.Rows[dgvResults.CurrentCell.RowIndex].Cells["Move2"].Value.ToString());
            }
        }

        private void searchBackToolStripMenuItem_Click(object sender, EventArgs e) {
            frmMain.PlaySearchMoveBackward();
        }

        private void LoadGameList(string pid, string move, string gameType, string result) {
            if(frmMain.frmGameLists == null || frmMain.frmGameLists.IsDisposed) {
                frmMain.frmGameLists = new FrmGameLists(frmMain);
            }
            if(move != string.Empty) {
                frmMain.frmGameLists.LoadGameList(pid, move, gameType, result);
            } else {
                frmMain.frmGameLists.LoadGameList(pid, gameType, result);
            }
            frmMain.frmGameLists.Show();
            frmMain.frmGameLists.Activate();
        }

        private void allToolStripMenuItem1_Click(object sender, EventArgs e) {
            string move = "...";
            if(dgvResults.Rows.Count > 0) {
                move = dgvResults.Rows[dgvResults.CurrentCell.RowIndex].Cells["Move2"].Value.ToString();
            }
            LoadGameList(this.pid, move, gameType, "");
        }

        private void whiteWinsToolStripMenuItem1_Click(object sender, EventArgs e) {
            string move = "...";
            if(dgvResults.Rows.Count > 0) {
                move = dgvResults.Rows[dgvResults.CurrentCell.RowIndex].Cells["Move2"].Value.ToString();
            }
            LoadGameList(this.pid, move, gameType, "1-0");
        }

        private void drawToolStripMenuItem1_Click(object sender, EventArgs e) {
            string move = "...";
            if(dgvResults.Rows.Count > 0) {
                move = dgvResults.Rows[dgvResults.CurrentCell.RowIndex].Cells["Move2"].Value.ToString();
            }
            LoadGameList(this.pid, move, gameType, "1/2-1/2");
        }

        private void blackWinsToolStripMenuItem1_Click(object sender, EventArgs e) {
            string move = "...";
            if(dgvResults.Rows.Count > 0) {
                move = dgvResults.Rows[dgvResults.CurrentCell.RowIndex].Cells["Move2"].Value.ToString();
            }
            LoadGameList(this.pid, move, gameType, "0-1");
        }

        private void allToolStripMenuItem2_Click(object sender, EventArgs e) {
            LoadGameList(this.pid, "", gameType, "");
        }

        private void whiteWinsToolStripMenuItem2_Click(object sender, EventArgs e) {
            LoadGameList(this.pid, "", gameType, "1-0");
        }

        private void drawToolStripMenuItem2_Click(object sender, EventArgs e) {
            LoadGameList(this.pid, "", gameType, "1/2-1/2");
        }

        private void blackWinsToolStripMenuItem2_Click(object sender, EventArgs e) {
            LoadGameList(this.pid, "", gameType, "0-1");
        }
    }
}
